<template>
    <div class="sidebar">
        <el-menu
            class="sidebar-el-menu"
            :default-active="onRoutes"
            :collapse="collapse"
            background-color="#324157"
            text-color="#bfcbd9"
            active-text-color="#20a0ff"
            unique-opened
            router
        >
            <template v-for="item in items">
                <template v-if="item.subs">
                    <el-submenu :index="item.index" :key="item.index">
                        <template slot="title">
                            <i :class="item.icon"></i>
                            <span slot="title">{{ item.title }}</span>
                        </template>
                        <template v-for="subItem in item.subs">
                            <el-submenu v-if="subItem.subs" :index="subItem.index" :key="subItem.index">
                                <template slot="title">{{ subItem.title }}</template>
                                <el-menu-item v-for="(threeItem, i) in subItem.subs" :key="i" :index="threeItem.index">{{
                                    threeItem.title
                                }}</el-menu-item>
                            </el-submenu>
                            <el-menu-item v-else :index="subItem.index" :key="subItem.index">{{ subItem.title }}</el-menu-item>
                        </template>
                    </el-submenu>
                </template>
                <template v-else>
                    <el-menu-item :index="item.index" :key="item.index">
                        <i :class="item.icon"></i>
                        <span slot="title">{{ item.title }}</span>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script>
import bus from '../common/bus';
export default {
    data() {
        return {
            collapse: false,
            items: [
                {
                    icon: 'el-icon-lx-home',
                    index: 'dashboard',
                    title: '系统首页'
                },
                // 部门
                {
                    icon: 'el-icon-lx-sort',
                    index: '1',
                    title: '部门管理',
                    subs: [
                        {
                            index: 'depts',
                            title: '部门查询'
                        },
                        {
                            index: 'addDept',
                            title: '部门添加'
                        }
                    ]
                },
                // 职位
                {
                    icon: 'el-icon-rank',
                    index: '2',
                    title: '职位管理',
                    subs: [
                        {
                            index: 'positions',
                            title: '职位查询'
                        },
                        {
                            index: 'addPosition',
                            title: '职位添加'
                        }
                    ]
                },
                // 员工
                {
                    icon: 'el-icon-lx-group',
                    index: '3',
                    title: '员工管理',
                    subs: [
                        {
                            index: 'employees',
                            title: '员工查询'
                        },
                        {
                            index: 'addEmployee',
                            title: '员工添加'
                        }
                    ]
                },
                // 用户
                {
                    icon: 'el-icon-user',
                    index: '4',
                    title: '用户管理',
                    subs: [
                        {
                            index: 'users',
                            title: '用户查询'
                        },
                        {
                            index: 'addUser',
                            title: '用户添加'
                        }
                    ]
                },
                // 权限
                {
                    icon: 'el-icon-s-cooperation',
                    index: '5',
                    title: '权限资源管理',
                    subs: [
                        {
                            index: 'roles',
                            title: '角色管理'
                        },{
                            index: 'pers',
                            title: '权限管理'
                        },
                         {
                            index: 'menus',
                            title: '菜单管理'
                        },
                      
                    ]
                },

                // 考勤
                {
                    icon: 'el-icon-time',
                    index: '6',
                    title: '考勤管理',
                    subs: [
                        {
                            index: 'checks',
                            title: '考勤查询'
                        },
                        {
                            index: 'holidays',
                            title: '假期查询'
                        }
                    ]
                },

                 // 工资
                {
                    icon: 'el-icon-lx-recharge',
                    index: '7',
                    title: '工资管理',
                    subs: [
                        {
                            index: 'params',
                            title: '参数查询'
                        },
                        {
                            index: 'salarys',
                            title: '工资查询'
                        }
                    ]
                },

                 // 消息管理
                {
                    icon: 'el-icon-chat-line-square',
                    index: '8',
                    title: '消息管理',
                    subs: [
                        {
                            index: 'messages',
                            title: '信息查询'
                        },
                        {
                            index: 'addMessage',
                            title: '发送消息'
                        },
                         {
                            index: 'mymessage',
                            title: '个人消息'
                        },
                        {
                            index: 'leaves',
                            title: '假条查询'
                        },
                         {
                            index: 'addLeave',
                            title: '请假'
                        }
                    ]
                },            
                {
                    icon: 'el-icon-lx-emoji',
                    index: 'icon',
                    title: '自定义图标'
                },
                {
                    icon: 'el-icon-pie-chart',
                    index: 'charts',
                    title: 'schart图表'
                },
              
               
                {
                    icon: 'el-icon-lx-warn',
                    index: '97',
                    title: '错误处理',
                    subs: [
                        {
                            index: 'permission',
                            title: '权限测试'
                        },
                        {
                            index: '404',
                            title: '404页面'
                        }
                    ]
                },
               
            ],
            user:{},
            menus:[],
            roles:[],
        };
    },
    computed: {
        onRoutes() {
            return this.$route.path.replace('/', '');
        }
    },
    created() {
        // 通过 Event Bus 进行组件间通信，来折叠侧边栏
        bus.$on('collapse', (msg) => {
            this.collapse = msg;
            bus.$emit('collapse-content', msg);
        });
        this.getUser();
    },
    methods:{
        getUser(){
            this.user=JSON.parse(window.sessionStorage.getItem("user"))
            this.setMenus(this.user.roles)
        },

        setMenus(roles){
            this.menus=[];
            let c=[];
            for (let i = 0; i < roles.length; i++) {
                 c.push(roles[i].menus)
            }
            this.menus=this.unique(c);
            this.items=this.initMenus(this.menus)
        },

        initMenus(menus){
            let c =[ {
                    icon: 'el-icon-lx-home',
                    index: 'dashboard',
                    title: '系统首页'
                }];
            let d=1;
            for (let i = 0; i < menus.length; i++) {
               
               if(menus[i].level=='01'){
                   c.push({
                       icon:menus[i].icon,
                       index: d.toString(),
                       title:menus[i].name,
                       subs: this.initSubs(menus[i].id)
                   })
                   d=1+d;
               }   
            }
            return c;
        },

        initSubs(parentId){
            let sub=[];
            for (let i = 0; i < this.menus.length; i++) {
                if(this.menus[i].parentId==parentId){
                    sub.push({
                        index: this.menus[i].url,
                        title: this.menus[i].name
                    })
                } 
            }
            return sub;
        },

        unique(menus){ 
                let result = {}; 
                let finalResult=[]; 
                for(let i=0;i<menus.length;i++){ 
                result[menus[i].name]=menus[i];
            } 
            for(var item in result){ 
                finalResult.push(result[item]); 
            } 
            return finalResult[0]; 
        },

        



    }

    
};
</script>

<style scoped>
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
    width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
    width: 250px;
}
.sidebar > ul {
    height: 100%;
}
</style>
